package com.tour.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mysql.jdbc.StringUtils;
import com.tour.config.ResponseWrapper;
import com.tour.entity.DFood;
import com.tour.entity.Restaurant;
import com.tour.mapper.DFoodMapper;
import com.tour.mapper.FCommentMapper;
import com.tour.mapper.RestaurantMapper;
import com.tour.service.RestaurantService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tour.utils.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lel
 * @since 2021-03-30
 */
@Service
public class RestaurantServiceImpl extends ServiceImpl<RestaurantMapper, Restaurant> implements RestaurantService {

    @Autowired
    RestaurantMapper restaurantMapper;
    @Autowired
    FCommentMapper commentMapper;
    @Autowired
    DFoodMapper foodMapper;

    public ResponseWrapper addRestaurant(Restaurant restaurant){
        if (restaurant == null){
            return ResponseWrapper.markError();
        }else {
            int result = restaurantMapper.insert(restaurant);
            if (result == 1){
                return ResponseWrapper.markCustom(true,200,"增加成功",null);
            }else {
                return ResponseWrapper.markError();
            }
        }
    }

    public ResponseWrapper deleteRestaurant(Integer rid){
        if (rid == null){
            return ResponseWrapper.markError();
        }else {
            int result = restaurantMapper.deleteById(rid);
            if (result == 1){
                return ResponseWrapper.markCustom(true,200,"删除成功",null);
            }else {
                return ResponseWrapper.markError();
            }
        }
    }
    public ResponseWrapper updateRestaurant(Restaurant restaurant){
        if (restaurant == null){
            return ResponseWrapper.markError();
        }else {
            int result = restaurantMapper.updateById(restaurant);
            if (result == 1){
                return ResponseWrapper.markCustom(true,200,"修改成功",null);
            }else {
                return ResponseWrapper.markError();
            }
        }
    }
    public ResponseWrapper findAllRestaurant(String name,Integer pageSize,Integer pageNum,String city){
        System.out.println(city);
        QueryWrapper<Restaurant> restaurantQueryWrapper = new QueryWrapper<>();
        restaurantQueryWrapper.eq(!StringUtils.isNullOrEmpty(city),"city",city).like(!StringUtils.isNullOrEmpty(name),"shopname",name).orderByDesc("shop_score");
        List<Restaurant> list = restaurantMapper.selectList(restaurantQueryWrapper);
        if (list.size() > 0){
            ArrayList<Object> dateList = new ArrayList<>();
            for (Restaurant restaurant : list){
                HashMap<String, Object> restaurantMap = new HashMap<>();
                QueryWrapper<DFood> dFoodQueryWrapper = new QueryWrapper<>();
                dFoodQueryWrapper.eq("r_id",restaurant.getRId()).last("limit 0 , 3");
                List<DFood> dFoods = foodMapper.selectList(dFoodQueryWrapper);
                List<DFood> dFoodList = new ArrayList<>();
                for (DFood dFood :dFoods){
                    dFoodList.add(dFood);
                }
                restaurantMap.put("restaurant",restaurant);
                restaurantMap.put("dFoods",dFoodList);
                dateList.add(restaurantMap);
            }
            List<Object> List = PageUtil.startPage(dateList,pageNum,pageSize);
            ResponseWrapper wrapper = ResponseWrapper.markSuccess(List);
            wrapper.getData().put("data",List);
            wrapper.getData().put("total",dateList.size());
            return wrapper;
        }else {
            return ResponseWrapper.markCustom(false,500,"没有数据",null);
        }

    }

    public ResponseWrapper getRestBysId(Integer rid, Integer pageSize,Integer pageNum,Integer foodpageSize,Integer foodpageNum){
        Restaurant restaurant = restaurantMapper.selectById(rid);
        Page<Map> page = new Page<>(pageNum,pageSize);
        IPage<Map> iPage = commentMapper.findCommentByrid(page,rid);
        List<Map> list = iPage.getRecords();
        ResponseWrapper wrapper = ResponseWrapper.markSuccess(list);
        wrapper.getData().put("restaurant",restaurant);
        wrapper.getData().put("comment",list);
        wrapper.getData().put("total",iPage.getTotal());
        wrapper.getData().put("pageSize",iPage.getSize());
        wrapper.getData().put("pageNum",iPage.getCurrent());
        QueryWrapper<DFood> foodQueryWrapper = new QueryWrapper<>();
        foodQueryWrapper.eq("r_id",rid).orderByDesc("f_id");
        Page<DFood> foodPage = new Page<>(foodpageNum,foodpageSize);
        IPage<DFood> foodIPage = foodMapper.selectPage(foodPage,foodQueryWrapper);
        List<DFood> foodList = foodIPage.getRecords();
        wrapper.getData().put("foodList",foodList);
        wrapper.getData().put("foodtotal",foodIPage.getTotal());
        wrapper.getData().put("foodpageSize",foodIPage.getSize());
        wrapper.getData().put("foodpageNum",foodIPage.getCurrent());
        return wrapper;
    }

}
